﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Threading;

namespace WPFTaskbar.Common
{
    internal sealed class FlashWindowHelper
    {
        DispatcherTimer _timer;
        int _count = 0;
        int _maxTimes = 0;
        Window _window;

        public void Flash(int times, double millliseconds, Window window)
        {
            _timer = new DispatcherTimer();
            _maxTimes = times;
            _timer.Interval = TimeSpan.FromMilliseconds(millliseconds);
            _timer.Tick += OnTick;
            _window = window;
            _timer.Start();
        }

        void OnTick(object sender, EventArgs e)
        {
            if (++_count < _maxTimes)
            {
                Win32.FlashWindow(new WindowInteropHelper(_window).Handle, (_count % 2) == 0);
            }
            else
            {
                _timer.Stop();
            }
        }
    }

    internal static class Win32
    {
        [DllImport("user32.dll")]
        public static extern bool FlashWindow(IntPtr hwnd, bool bInvert);
    }
}
